package com.yanjue.config;


import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
@ConfigurationProperties(prefix = "es")
@RefreshScope
public class JestConfig {
    private static Logger LOGGER = LoggerFactory.getLogger(JestConfig.class);

   // @Value("${es.servers}")
    private String servers;

    /** 索引别名 */
    //@Value("${es.aliasName}")
    private String aliasName;

    /** 类型 */
    //@Value("${es.indexType}")
    private String indexType;

    @Bean
    public JestClient jestClient() {
        JestClientFactory factory = new JestClientFactory();
        String[] sers = servers.split(",");
        HttpClientConfig httpClientConfig =
                new HttpClientConfig.Builder(Arrays.asList(sers))
                        .multiThreaded(true)
                        .connTimeout(5000)
                        .readTimeout(3000)
                        .maxTotalConnection(10)
                        .defaultMaxTotalConnectionPerRoute(10)
                        .build();
        factory.setHttpClientConfig(httpClientConfig);
        LOGGER.info("es_servers:{}", servers);
        return factory.getObject();
    }
}
